package com.ympc.service.impl;

import com.ympc.mapper.AssignmentMapper;
import com.ympc.mapper.ClassMapper;
import com.ympc.mapper.StudentAssignmentMapper;
import com.ympc.mapper.StudentMapper;
import com.ympc.pojo.Assignment;
import com.ympc.pojo.Class;
import com.ympc.pojo.Student;
import com.ympc.pojo.StudentAssignment;
import com.ympc.pojo.StudentCompletionStatus;
import com.ympc.service.StudentAssignmentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentAssignmentServiceImpl implements StudentAssignmentService {
    
    @Resource
    private StudentAssignmentMapper studentAssignmentMapper;
    
    @Resource
    private AssignmentMapper assignmentMapper;
    
    @Resource
    private StudentMapper studentMapper;
    
    @Resource
    private ClassMapper classMapper;
    
    @Override
    public List<Assignment> getAssignmentsForStudent(Integer studentId) {
        // 获取学生信息
        Student student = studentMapper.findById(studentId);
        if (student == null || student.getClassName() == null) {
            return new ArrayList<>();
        }
        
        // 根据班级名称查找class_id
        List<Class> classes = classMapper.findAll();
        List<Integer> classIds = new ArrayList<>();
        for (Class clazz : classes) {
            if (clazz.getClassName().equals(student.getClassName())) {
                classIds.add(clazz.getId());
            }
        }
        
        if (classIds.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取该班级所有已发布的作业
        List<Assignment> assignments = new ArrayList<>();
        for (Integer classId : classIds) {
            List<Assignment> classAssignments = assignmentMapper.findByClassIdAndStatus(classId, "published");
            assignments.addAll(classAssignments);
        }
        
        return assignments;
    }
    
    @Override
    public List<StudentAssignment> getSubmittedAssignments(Integer studentId) {
        return studentAssignmentMapper.findByStudentId(studentId);
    }
    
    @Override
    public StudentAssignment getSubmission(Integer assignmentId, Integer studentId) {
        return studentAssignmentMapper.findByAssignmentIdAndStudentId(assignmentId, studentId);
    }
    
    @Override
    public boolean submitAssignment(StudentAssignment studentAssignment) {
        // 检查是否已提交
        StudentAssignment existing = studentAssignmentMapper.findByAssignmentIdAndStudentId(
            studentAssignment.getAssignmentId(), 
            studentAssignment.getStudentId()
        );
        
        if (existing != null) {
            // 已提交，更新记录
            studentAssignment.setId(existing.getId());
            return studentAssignmentMapper.update(studentAssignment) > 0;
        } else {
            // 首次提交，插入记录
            return studentAssignmentMapper.insert(studentAssignment) > 0;
        }
    }
    
    @Override
    public boolean updateSubmission(StudentAssignment studentAssignment) {
        return studentAssignmentMapper.update(studentAssignment) > 0;
    }
    
    @Override
    public List<StudentAssignment> getSubmissionsByAssignmentId(Integer assignmentId) {
        return studentAssignmentMapper.findByAssignmentId(assignmentId);
    }
    
    @Override
    public boolean gradeAssignment(Integer submissionId, Integer score, String comment) {
        StudentAssignment submission = studentAssignmentMapper.findById(submissionId);
        if (submission == null) {
            return false;
        }
        submission.setScore(score);
        submission.setComment(comment);
        submission.setStatus("graded");
        return studentAssignmentMapper.update(submission) > 0;
    }
    
    @Override
    public List<StudentCompletionStatus> getStudentCompletionStatus(Integer assignmentId, String className) {
        // 获取该班级的所有学生
        List<Student> students = studentMapper.findByClassName(className);
        if (students == null || students.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取该作业的所有提交记录
        List<StudentAssignment> submissions = studentAssignmentMapper.findByAssignmentId(assignmentId);
        
        // 创建完成状态列表
        List<StudentCompletionStatus> statusList = new ArrayList<>();
        for (Student student : students) {
            StudentAssignment submission = null;
            String status = "未答题";
            
            // 查找该学生的提交记录
            for (StudentAssignment sub : submissions) {
                if (sub.getStudentId().equals(student.getId())) {
                    submission = sub;
                    if ("graded".equals(sub.getStatus())) {
                        status = "已批改";
                    } else {
                        status = "已提交";
                    }
                    break;
                }
            }
            
            statusList.add(new StudentCompletionStatus(student, status, submission));
        }
        
        return statusList;
    }
    
    @Override
    public StudentAssignment getSubmissionById(Integer submissionId) {
        return studentAssignmentMapper.findById(submissionId);
    }
}

